//
// Created by liuhuan on 2022/5/8.
//

#include "callback/CallBack.h"

void CallBack::init(JNIEnv *env, jobject call_back, JavaVM *vm) {
    m_env = env;
    jvm = vm;
    m_obj = env->NewGlobalRef(call_back);
    //找到对应的JNITools类
    clz = env->GetObjectClass(call_back);
    //2.jni调用java方法 类似反射
    initMap();
}

void CallBack::detectInfoCB(SKYNODE_GND_DETECTINFOS_S stru, std::vector<BoundingBox> boxs) {

}

void CallBack::baseInfoCB(SKYNODE_GND_BASEINFO_S stru) {

}

void CallBack::devInfoCB(SKYNODE_GND_DEVINFO_S stru, std::vector<DeviceInfo> vector) {
    JNIEnv *env;

    int status = jvm->GetEnv((void **) &env, JNI_VERSION_1_4);

    if (status < 0) {

        status = jvm->AttachCurrentThread(&env, NULL);

        if (status < 0) {

            return;

        }
    }
    jclass jClassName = m_env->FindClass("com/rykj/socket_lib/entity/SkyNodeGndDevice");

//    //创建对象
//    jmethodID initId = env->GetMethodID(jClassName, "<init>", "()V");
//    //创建对象
//    jobject person = env->NewObject(jClassName, initId);
//
//    JniFieldUtils::SetCharFieldVale(env, jClassName, person, "isRanging", stru.isRanging);
//    JniFieldUtils::SetCharFieldVale(env, jClassName, person, "devNums", stru.devNums);
//
//
//    jclass list_jcs = env->FindClass("java/util/ArrayList");
//    if (list_jcs == NULL) {
//        printf("%s\n", "ArrayList no  find!");
//        return;
//    }
//    //创建一个ArrayList对象
//    jobject list_obj = JniFieldUtils::createVoidObject(env, list_jcs);
//
//    //获取ArrayList对象的add()的methodID
//    jmethodID list_add = env->GetMethodID(list_jcs, "add", "(Ljava/lang/Object;)Z");
//
//    //获取DeviceInfo类
//    jclass DeviceInfo_cls = env->FindClass("com/rykj/socket_lib/entity/DeviceInfo");
//    if (DeviceInfo_cls == NULL) {
//        printf("%s\n", "Path Class not find!");
//        return;
//    }
//    //获取Path的构造函数
//    jmethodID DeviceInfo_init = env->GetMethodID(DeviceInfo_cls, "<init>", "()V");  //括号里为签名见下图
//    if (DeviceInfo_init == NULL) {
//        printf("%s\n", "Path init not find!");
//        return;
//    }
//
//    for (DeviceInfo info  : vector) {
//        jobject path_obj = env->NewObject(DeviceInfo_cls, DeviceInfo_init);
//        JniFieldUtils::SetCharFieldVale(env, DeviceInfo_cls, path_obj, "no", info.no);
//        JniFieldUtils::SetCharFieldVale(env, DeviceInfo_cls, path_obj, "camType", info.camType);
//        JniFieldUtils::SetCharFieldVale(env, DeviceInfo_cls, path_obj, "devStatus",
//                                        info.devStatus);
//        JniFieldUtils::SetShortFieldVale(env, DeviceInfo_cls, path_obj, "port", info.port);
//        env->CallBooleanMethod(list_obj, list_add, path_obj);
//        env->DeleteLocalRef(path_obj);
//    }
//    jfieldID name_id = env->GetFieldID(jClassName, "deviceInfos", "Ljava/util/List;");
//    env->SetObjectField(person, name_id, list_obj);
//
////    m_env->CallVoidMethod(m_obj, methodIdMap["onBaseInfoCallBack"], person);
//    env->DeleteLocalRef(list_obj);
//    env->DeleteLocalRef(person);
    jvm->DetachCurrentThread();
}

void CallBack::trackInfoCB(SKYNODE_GND_TRACKINFO_S stru) {

}

void CallBack::ptzPIDInfoCB(SKYNODE_GND_PTZ_PID_S stru) {

}

void CallBack::algParamInfoCB(SKYNODE_GND_ALG_PARAM_S stru) {

}


void CallBack::test() {
//    jclass jClassName = m_env->FindClass("com/rykj/socket_lib/entity/SkyNodeGndPTZPid");
//    jmethodID initId = m_env->GetMethodID(jClassName, "<init>", "()V");
//    //创建对象
//    jobject person = m_env->NewObject(jClassName, initId);
//    JniFieldUtils::SetShortFieldVale(m_env, jClassName, person, "pitchP", 20);
//    m_env->CallVoidMethod(m_obj, methodIdMap["onPIDCallBack"], person);
//    //创建的对象一定要释放
//    m_env->DeleteLocalRef(person);
}

void CallBack::initMap() {
    jmethodID onPIDCallBackId = m_env->GetMethodID(clz, "onPIDCallBack",
                                                   "(Lcom/rykj/socket_lib/entity/SkyNodeGndPTZPid;)V");
    if (!onPIDCallBackId) {
        return;
    }
    methodIdMap["onPIDCallBack"] = onPIDCallBackId;

    jmethodID onDevInfoCallBackId = m_env->GetMethodID(clz, "onDevInfoCallBack",
                                                       "(Lcom/rykj/socket_lib/entity/SkyNodeGndDevice;)V");
    if (!onDevInfoCallBackId) {
        return;
    }
    methodIdMap["onDevInfoCallBack"] = onDevInfoCallBackId;

    jmethodID onBaseInfoCallBackId = m_env->GetMethodID(clz, "onBaseInfoCallBack",
                                                        "(Lcom/rykj/socket_lib/entity/SkyNodeGndBaseInfo;)V");
    if (!onBaseInfoCallBackId) {
        return;
    }
    methodIdMap["onBaseInfoCallBack"] = onBaseInfoCallBackId;

    jmethodID onDetectInfoCallBackId = m_env->GetMethodID(clz, "onDetectInfoCallBack",
                                                          "(Lcom/rykj/socket_lib/entity/SkyNodeGndDetectInfoS;)V");
    if (!onDetectInfoCallBackId) {
        return;
    }
    methodIdMap["onDetectInfoCallBack"] = onDetectInfoCallBackId;

    jmethodID onTrackInfoCallBackId = m_env->GetMethodID(clz, "onTrackInfoCallBack",
                                                         "(Lcom/rykj/socket_lib/entity/SkyNodeGndTrackInfo;)V");
    if (!onTrackInfoCallBackId) {
        return;
    }
    methodIdMap["onTrackInfoCallBack"] = onTrackInfoCallBackId;

    jmethodID onAlgParamInfoCallBackId = m_env->GetMethodID(clz, "onAlgParamInfoCallBack",
                                                            "(Lcom/rykj/socket_lib/entity/SkyNodeGndAlgParams;)V");
    if (!onAlgParamInfoCallBackId) {
        return;
    }
    methodIdMap["onAlgParamInfoCallBack"] = onAlgParamInfoCallBackId;
}

void CallBack::initClass() {
    m_env->FindClass("com/rykj/socket_lib/entity/SkyNodeGndPTZPid");
    m_env->FindClass("com/rykj/socket_lib/entity/BoundingBox");
    m_env->FindClass("com/rykj/socket_lib/entity/DetectParams");
    m_env->FindClass("com/rykj/socket_lib/entity/DeviceInfo");
    m_env->FindClass("com/rykj/socket_lib/entity/PidInfo");
    m_env->FindClass("com/rykj/socket_lib/entity/SkyNodeGndAlgParams");
    m_env->FindClass("com/rykj/socket_lib/entity/SkyNodeGndBaseInfo");
    m_env->FindClass("com/rykj/socket_lib/entity/SkyNodeGndDetectInfoS");
    m_env->FindClass("com/rykj/socket_lib/entity/SkyNodeGndDevice");
    m_env->FindClass("com/rykj/socket_lib/entity/SkyNodeGndPTZPid");
    m_env->FindClass("com/rykj/socket_lib/entity/SkyNodeGndTrackInfo");
    m_env->FindClass("java/util/ArrayList");
}

void CallBack::initMethod() {

}

void CallBack::initField() {

}
